package com.seabook.calparser.util;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;

import com.seabook.calparser.pojo.CalParserConfig;

public class URIBuilder {

	private URIBuilder() {

	}

	public static String buildURL(CalParserConfig config) {
		List<NameValuePair> qparams = new ArrayList<NameValuePair>();
		qparams.add(new BasicNameValuePair(CalParserConfig.REQUEST_COMMAND,
				config.getCommand()));
		qparams.add(new BasicNameValuePair(CalParserConfig.REQUEST_PERIOD,
				config.getPeriod()));
		qparams.add(new BasicNameValuePair(CalParserConfig.REQUEST_RESOLUTION,
				config.getResolution()));
		qparams.add(new BasicNameValuePair(CalParserConfig.REQUEST_WHEN, config
				.getWhen()));
		qparams.add(new BasicNameValuePair(CalParserConfig.REQUEST_ROW, config
				.getRow()));
		qparams.add(new BasicNameValuePair(CalParserConfig.REQUEST_SCHEMA,
				config.getSchema()));
		qparams
				.add(new BasicNameValuePair(
						CalParserConfig.REQUEST_ENVIRONMENTS, config
								.getEnvironments()));
		qparams.add(new BasicNameValuePair(CalParserConfig.REQUEST_POOL, config
				.getPool()));
		URI uri = null;
		try {
			uri = URIUtils.createURI("http", CalParserConfig.CAL_REPORT_HOST,
					80, CalParserConfig.REQUEST_PATH, URLEncodedUtils.format(
							qparams, "UTF-8"), null);
		} catch (URISyntaxException e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
		return uri.toString();
	}
}
